﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Windows.Forms;

namespace Bjsubway.Xml
{
    public class LineTime
    {
        public string LineName
        {
            get;
            set;
        }
        public string FromTo1
        {
            get;
            set;
        }
        public string Half1
        {
            get;
            set;
        }
        public string FromTo2
        {
            get;
            set;
        }
        public string Half2
        {
            get;
            set;
        }
        public List<Station> Stations
        {
            get;
            set;
        }

        public static List<LineTime> GetLineTimeFromXml(string xml)
        {
            string currentdir =Application.StartupPath;

            string lineTimeXmlPath = currentdir.Substring(0,currentdir.LastIndexOf("\\")) +"\\data\\" + xml;
            try
            {
                XDocument pList = XDocument.Load(lineTimeXmlPath);
                var linetime = from a in pList.Descendants("root").Elements("line")
                               select new LineTime
                               {
                                   LineName = a.Attribute("name").Value,
                                   FromTo1 = (a.Attribute("fromTo1")==null)?"":a.Attribute("fromTo1").Value,
                                   FromTo2 = (a.Attribute("fromTo2") == null) ? "" : a.Attribute("fromTo2").Value,
                                   Half1 = (a.Attribute("half1") == null) ? "" : a.Attribute("half1").Value,
                                   Half2 = (a.Attribute("half2") == null) ? "" : a.Attribute("half2").Value,
                                   Stations = (from s in a.Elements("station")
                                              select new Station
                                              {
                                                  Name= s.Attribute("name").Value,
                                                  Time1 = s.Attribute("time1").Value,
                                                  Time2 = s.Attribute("time2").Value,
                                                  HalfLast1 = (s.Attribute("halfLast1") == null) ? null : s.Attribute("halfLast1").Value,
                                                  HalfLast2 = (s.Attribute("halfLast2") == null) ? null : s.Attribute("halfLast2").Value,
                                              }).ToList()
                               };
                if (linetime == null) return null;
                return linetime.ToList();
            }
            catch (Exception e)
            {
                return null;
            }
        }
    }
}
